/*
 * @Author: AskMeWhy
 * @Date: 2020-12-27 11:58:47
 * @Last Modified by: AskMeWhy
 * @Last Modified time: 2021-02-05 11:09:18
 */
const app = getApp();
const { AskComponent } = app._require("common/index");
AskComponent({
    properties: {
        list: {
            type: Array,
            value: [],
        },
    },
    data: {
        share: {
            show: false,
            item: null,
            list: [
                // { name: "组织内", value: "orgIn" },
                { name: "转发给其他人填报", value: "orgOut", openType: "share", taskId: "" },
            ],
        },
    },
    methods: {
        onShare(e) {
            let { value: item } = e.detail;
            let auth = this.checkAuth();
            if (!auth) return;
            this.data.share.item = item;
            this.setData({
                "share.show": true,
            });
        },
        onShareChange(e) {
            let { value } = e.detail;
            if (value === "orgIn") {
                wx.navigateTo({
                    url: "/pages/plugins/transfer-org/index",
                    events: {
                        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
                        sure: data => {
                            this.sendTask({ detail: data });
                        },
                    },
                    success: res => {
                        // 通过eventChannel向被打开页面传送数据
                        // res.eventChannel.emit("initData", {
                        //     item: this.data.share.item,
                        // });
                    },
                });
            }
            console.log("onShareChange", e);
        },
        onShareClose() {
            this.setData({
                "share.show": false,
            });
        },
        onFill(e) {
            let auth = this.checkAuth();
            if (!auth) return;
            let { value: item } = e.detail;
            let _url = "/pages/task-fill-list/index";
            wx.navigateTo({
                url: `${_url}?readonly=${ item.node.type === 'REVIEW'  || item.state.value !== "EXECUTING" || item.readonly ? 1 : 0}&taskId=${
                    item.id || ""
                }`,
            });
        },
        updateTask(e){
            let auth = this.checkAuth();
            if (!auth) return;
            let { value: item } = e.detail;
            const _page = this.getPage();
            let status = {
                value: "",
            };
            let info = {
                msg: "任务已完成",
                errMsg: "状态修改失败",
            };
            if (_page) {
                status = _page.$key2Name(
                    {
                        type: "EXECUTING",
                        name: "state",
                        list: _page.data.taskStateList,
                    },
                    "obj"
                );
            }
            this.updateAudit(item, status.state, info, () => {
                const _page = this.getPage();
                if (_page) {
                    _page.loginSuccess();
                }
                this.onFill({
                    detail: {
                        value: {
                            ...item,
                            state: status,
                        },
                    },
                });
            });
        },
        async onEnd(e){
            console.log("eee");
            // let auth = this.checkAuth();
            // if (!auth) return;
            // let { value: item } = e.detail;
            // let info = {
            //     msg: "任务已完成",
            //     errMsg: "状态修改失败"
            // }
            // try {
            //     const response = await this.$_server("task/finish", {
            //         data: {
            //             taskId: item.id
            //         },
            //     });
            //     let { data } = response;
            //     if (data.code !== 200) {
            //         throw data;
            //     }
            //     wx.askShowToastEx({
            //         mask: true,
            //         title: info.msg || "状态已更新",
            //         icon: "none",
            //         duration: 2000,
            //         cancel: () => {
            //             const _page = this.getPage();
            //             if (_page) {
            //                 _page.loginSuccess();
            //             }
            //         },
            //     });
            // } catch (error) {
            //     console.log(error);
            //     // statements
            //     if (error.message) {
            //         wx.showToast({
            //             mask: true,
            //             title: error.message || info.errMsg,
            //             icon: "none",
            //             duration: 2000,
            //         });
            //     } else {
            //         throw error;
            //     }
            // }
        },
        async updateAudit(item, status, info, callback){
         
            // try {
            //     const response = await this.$_server("task/audit", {
            //         data: {
            //             id: item.id,
            //             status
            //         }
            //     });
            //     let { data } = response;
            //     if (data.code !== 200) {
            //         throw data;
            //     }
            //     if(callback){
            //         callback();
            //     }else{
            //         wx.askShowToastEx({
            //             mask: true,
            //             title: info.msg || "状态已更新",
            //             icon: "none",
            //             duration: 2000,
            //             cancel: () => {
            //                 const _page = this.getPage();
            //                 if (_page) {
            //                     _page.loginSuccess();
            //                 }
            //             },
            //         });
            //     }
            // } catch (error) {
            //     console.log(error);
            //     // statements
            //     if (error.message) {
            //         wx.showToast({
            //             mask: true,
            //             title: error.message || info.errMsg,
            //             icon: "none",
            //             duration: 2000,
            //         });
            //     } else {
            //         throw error;
            //     }
            // }
        },
        async sendTask(e){
            try {
                let { actives }= e.detail;
                let { item } = this.data.share;
                const response = await this.$_server("task/sendTask", {
                    data: {
                        id: item.id,
                        userId: actives.join(",")
                    }
                });
                let { data } = response;
                if (data.code !== 200) {
                    throw data;
                }
                wx.askShowToastEx({
                    mask: true,
                    title: "任务已转发",
                    duration: 2000,
                    icon: "none",
                    cancel: () => {
                        const _page = this.getPage();
                        if (_page) {
                            _page.loginSuccess();
                        }
                    },
                });
            } catch (error) {
                console.log(error);
                // statements
                if (error.message) {
                    wx.showToast({
                        mask: true,
                        title: error.message,
                        icon: "none",
                        duration: 2000,
                    });
                } else {
                    throw error;
                }
            }
        },
    },
});
